package com.lhy.vaccine_stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhy.vaccine_stock.entity.SysRegion;
import com.lhy.vaccine_stock.entity.VaccineStock;
import com.lhy.vaccine_stock.form.SearchForm;
import com.lhy.vaccine_stock.mapper.VaccineStockMapper;
import com.lhy.vaccine_stock.service.IVaccineStockService;
import com.lhy.vaccine_stock.vo.PageVo;
import com.lhy.vaccine_stock.vo.R;
import com.lhy.vaccine_stock.vo.VaccineMsgVo;
import com.lhy.vaccine_stock.vo.VaccineOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhy
 * @since 2023-02-22
 */
@Service
public class VaccineStockService extends ServiceImpl<VaccineStockMapper, VaccineStock> implements IVaccineStockService {

    @Autowired
    VaccineStockMapper vaccineStockMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public PageVo listByPage(Integer currentPage, Integer pageSize) {

        Page<VaccineStock> page = new Page<>(currentPage,pageSize);
        Page<VaccineStock> stockPage = vaccineStockMapper.selectPage(page, null);

        PageVo pageVo=new PageVo();

        pageVo.setTotal(stockPage.getTotal());
        pageVo.setData(stockPage.getRecords());

        return pageVo;
    }

    private Page<VaccineStock> getSelectPage(Page<VaccineStock> page) {

        return vaccineStockMapper.selectPage(page, null);
    }

    private List<VaccineStock> getVaccineAll(){
        List<VaccineStock> vaccineStocks = vaccineStockMapper.selectList(new QueryWrapper<>());
        return vaccineStocks;
    }


    @Override
    public PageVo<SearchForm> search(SearchForm searchForm) {
        Page<VaccineStock> page=new Page<>(searchForm.getPage(), searchForm.getPageSize());

        Page<VaccineStock> searchFormPage = null;
        if (searchForm.getValue().equals("")){
            searchFormPage= getSelectPage(page);
        }else{
            QueryWrapper<VaccineStock> wrapper = new QueryWrapper<>();
            wrapper.like(searchForm.getKey(),searchForm.getValue());

            searchFormPage=vaccineStockMapper.selectPage(page,wrapper);
        }

        PageVo pageVo = new PageVo<>();
        pageVo.setData(searchFormPage.getRecords());
        pageVo.setTotal(searchFormPage.getTotal());
        return pageVo;
    }

    @Override
    public R getVaccineList() {

        List<String> result = vaccineStockMapper.getVaccineList();
        if (result.size()>0 && result!=null){
            return R.ok().put("data",result);
        }
        return R.error();
    }

    @Override
    public R getVaccineNumber(String newValue) {
        List<String> stringList = vaccineStockMapper.getVaccineNumber(newValue);
        if (stringList.size()>0 && stringList!=null){
            return R.ok().put("data",stringList);
        }
        return R.error();
    }

    @Transactional
    @Override
    public synchronized R getVaccineId(VaccineOrderVo vaccineOrderVo) {
        VaccineStock vaccineStock = baseMapper.selectById(vaccineOrderVo.getVId());
        Long stock = Long.valueOf(vaccineStock.getVStock());
        int update = 0;

        if(stock > 0) {
            stock = stock - 1;
            VaccineStock vaccine = new VaccineStock();
            vaccine.setVId(vaccineStock.getVId());
            vaccine.setVMsg(vaccineStock.getVMsg());
            vaccine.setVName(vaccineStock.getVName());
            vaccine.setVNumber(vaccineStock.getVNumber());
            vaccine.setVStock(String.valueOf(stock));

            UpdateWrapper<VaccineStock> wrapper = new UpdateWrapper<VaccineStock>().
                    set("v_stock", String.valueOf(stock)).eq("v_id", vaccineStock.getVId());
            update = baseMapper.update(vaccine, wrapper);
        }

        if (update == 1){
            return R.ok().put("data",update);
        }
        return R.error().put("data","库存不足");
    }


}
